package com.sugon.controller.device;

import com.sugon.annotation.AutoLog;
import com.sugon.annotation.DataFilter;
import com.sugon.entity.DevLeaveRecordEntity;
import com.sugon.modules.device.service.IDeviceManager;
import com.sugon.service.DevLeaveRecordService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备闲置记录Controller
 *
 * @author sugon
 * @date 2019-07-12 08:49:29
 */
@Controller
@RequestMapping("devleaverecord")
public class DevLeaveRecordController {

    @Autowired
    private DevLeaveRecordService devLeaveRecordService;
    @Autowired
    private IDeviceManager deviceManager;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("devleaverecord:list")
    @ResponseBody
    @DataFilter(deptAlias = "WORKSHOP")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<DevLeaveRecordEntity> devLeaveRecordList = devLeaveRecordService.queryList(query);
        int total = devLeaveRecordService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(devLeaveRecordList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("devleaverecord:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevLeaveRecordEntity devLeaveRecord = devLeaveRecordService.queryObject(id);

        return R.ok().put("devLeaveRecord", devLeaveRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("devleaverecord:save")
    @ResponseBody
    public R save(@RequestBody DevLeaveRecordEntity devLeaveRecord) {
        devLeaveRecordService.save(devLeaveRecord);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("devleaverecord:update")
    @ResponseBody
    public R update(@RequestBody DevLeaveRecordEntity devLeaveRecord) {
        devLeaveRecordService.update(devLeaveRecord);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("devleaverecord:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devLeaveRecordService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevLeaveRecordEntity> list = devLeaveRecordService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 新增设备闲置申请
     */
    @RequestMapping("/addApply")
    @RequiresPermissions("deviceinfo:devLive")
    @ResponseBody
    public R updateStuts(@RequestBody List<DevLeaveRecordEntity> param) {
        return deviceManager.addDevLeaveApply(param) ? R.ok() : R.error();
    }

    /**
     * 批量审核设备闲置
     */
    @RequestMapping("/batchReview")
    @RequiresPermissions("devleaverecord:review")
    @ResponseBody
    public R review(@RequestBody String[] devLeaveRecordIds) {
        return deviceManager.batchReviewDevLeaveApply(devLeaveRecordIds) ? R.ok() : R.error();
    }

    /**
     * 移除闲置
     * @param param
     * @return
     */
    @RequestMapping("/batchUpdate")
    @ResponseBody
    public R batchUpdate(@RequestBody List<DevLeaveRecordEntity> param) {

        devLeaveRecordService.remove(param);

        return R.ok();
    }
}
